using System;
using UnityEngine;

namespace MaterialUI
{
	[Serializable]
	public struct HSBColor
	{
		private float m_H;

		private float m_S;

		private float m_B;

		private float m_A;

		public float h
		{
			get
			{
				return m_H;
			}
			set
			{
				m_H = value;
			}
		}

		public float s
		{
			get
			{
				return m_S;
			}
			set
			{
				m_S = value;
			}
		}

		public float b
		{
			get
			{
				return m_B;
			}
			set
			{
				m_B = value;
			}
		}

		public float a
		{
			get
			{
				return m_A;
			}
			set
			{
				m_A = value;
			}
		}

		public HSBColor(float h, float s, float b, float a)
		{
			m_H = h;
			m_S = s;
			m_B = b;
			m_A = a;
		}

		public HSBColor(float h, float s, float b)
		{
			m_H = h;
			m_S = s;
			m_B = b;
			m_A = 1f;
		}

		public HSBColor(Color color)
		{
			HSBColor hSBColor = FromColor(color);
			m_H = hSBColor.m_H;
			m_S = hSBColor.m_S;
			m_B = hSBColor.m_B;
			m_A = hSBColor.m_A;
		}

		public static HSBColor FromColor(Color color)
		{
			HSBColor result = new HSBColor(0f, 0f, 0f, color.a);
			float r = color.r;
			float g = color.g;
			float b = color.b;
			float num = Mathf.Max(r, Mathf.Max(g, b));
			if (num <= 0f)
			{
				return result;
			}
			float num2 = Mathf.Min(r, Mathf.Min(g, b));
			float num3 = num - num2;
			if (num > num2)
			{
				if (g == num)
				{
					result.m_H = (b - r) / num3 * 60f + 120f;
				}
				else if (b == num)
				{
					result.m_H = (r - g) / num3 * 60f + 240f;
				}
				else if (b > g)
				{
					result.m_H = (g - b) / num3 * 60f + 360f;
				}
				else
				{
					result.m_H = (g - b) / num3 * 60f;
				}
				if (result.m_H < 0f)
				{
					result.m_H += 360f;
				}
			}
			else
			{
				result.m_H = 0f;
			}
			result.m_H *= 0.00277777785f;
			result.m_S = num3 / num * 1f;
			result.m_B = num;
			return result;
		}

		public static Color ToColor(HSBColor hsbColor)
		{
			float value = hsbColor.m_B;
			float value2 = hsbColor.m_B;
			float value3 = hsbColor.m_B;
			if (hsbColor.m_S != 0f)
			{
				float b = hsbColor.m_B;
				float num = hsbColor.m_B * hsbColor.m_S;
				float num2 = hsbColor.m_B - num;
				float num3 = hsbColor.m_H * 360f;
				if (num3 < 60f)
				{
					value = b;
					value2 = num3 * num / 60f + num2;
					value3 = num2;
				}
				else if (num3 < 120f)
				{
					value = (0f - (num3 - 120f)) * num / 60f + num2;
					value2 = b;
					value3 = num2;
				}
				else if (num3 < 180f)
				{
					value = num2;
					value2 = b;
					value3 = (num3 - 120f) * num / 60f + num2;
				}
				else if (num3 < 240f)
				{
					value = num2;
					value2 = (0f - (num3 - 240f)) * num / 60f + num2;
					value3 = b;
				}
				else if (num3 < 300f)
				{
					value = (num3 - 240f) * num / 60f + num2;
					value2 = num2;
					value3 = b;
				}
				else if (num3 <= 360f)
				{
					value = b;
					value2 = num2;
					value3 = (0f - (num3 - 360f)) * num / 60f + num2;
				}
				else
				{
					value = 0f;
					value2 = 0f;
					value3 = 0f;
				}
			}
			return new Color(Mathf.Clamp01(value), Mathf.Clamp01(value2), Mathf.Clamp01(value3), hsbColor.m_A);
		}

		public Color ToColor()
		{
			return ToColor(this);
		}

		public override string ToString()
		{
			return "H:" + m_H + " S:" + m_S + " B:" + m_B;
		}

		public static HSBColor Lerp(HSBColor a, HSBColor b, float t)
		{
			float h;
			float s;
			if (a.m_B == 0f)
			{
				h = b.m_H;
				s = b.m_S;
			}
			else if (b.m_B == 0f)
			{
				h = a.m_H;
				s = a.m_S;
			}
			else
			{
				if (a.m_S == 0f)
				{
					h = b.m_H;
				}
				else if (b.m_S == 0f)
				{
					h = a.m_H;
				}
				else
				{
					float num;
					for (num = Mathf.LerpAngle(a.m_H * 360f, b.m_H * 360f, t); num < 0f; num += 360f)
					{
					}
					while (num > 360f)
					{
						num -= 360f;
					}
					h = num / 360f;
				}
				s = Mathf.Lerp(a.m_S, b.m_S, t);
			}
			return new HSBColor(h, s, Mathf.Lerp(a.m_B, b.m_B, t), Mathf.Lerp(a.m_A, b.m_A, t));
		}
	}
}
